package com.alibaba.citrus.cr.order.open.api.facade.transferorder.impl;

import com.alibaba.citrus.cr.common.annotation.FacadeInvoker;
import com.alibaba.citrus.cr.order.open.api.facade.convert.TransferOrder2AllocateOrderConvert;
import com.alibaba.citrus.cr.order.open.api.facade.model.TransferOrder;
import com.alibaba.citrus.cr.order.open.api.facade.model.TransferOrderQueryRequest;
import com.alibaba.citrus.cr.order.open.api.facade.transferorder.TransferOrderQueryService;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.crunifiedinventory.domain.allocateorder.dto.AllocateOrderLoadListRequest;
import com.epoch.app.crunifiedinventory.domain.allocateorder.model.AllocateOrder;
import com.epoch.app.crunifiedinventory.domain.allocateorder.service.AllocateOrderService;
import com.epoch.app.crunifiedinventory.domain.allocateorderdetail.dto.AllocateOrderDetailLoadListRequest;
import com.epoch.app.crunifiedinventory.domain.allocateorderdetail.model.AllocateOrderDetail;
import com.epoch.app.crunifiedinventory.domain.allocateorderdetail.service.AllocateOrderDetailService;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @description:
 * @author: yuhw
 * @date: 2024年01月09日 16:23
 **/
@Service
@Primary
public class TransferOrderQueryServiceImpl implements TransferOrderQueryService {
    @Resource
    private AllocateOrderService allocateOrderService;
    @Resource
    private AllocateOrderDetailService allocateOrderDetailService;
    @Resource
    private TransferOrder2AllocateOrderConvert transferOrder2AllocateOrderConvert;
    private static final Integer MAX_PAGE_SIZE = 1000;

    @Override
    @FacadeInvoker(value = "调拨单查询详情")
    public Result<TransferOrder> query(TransferOrderQueryRequest request) {
        Result result = new Result();
        result.setSuccess(true);
        if (StringUtils.isBlank(request.getTransferOrderCode())){
            return result;
        }
        AllocateOrderLoadListRequest allocateOrderLoadListRequest = new AllocateOrderLoadListRequest();
        allocateOrderLoadListRequest.setCode(request.getTransferOrderCode());
        if (StringUtils.isNotBlank(request.getThirdAllocateOrderId())){
            allocateOrderLoadListRequest.setThirdAllocateOrderId(request.getThirdAllocateOrderId());
        }
        Result<List<AllocateOrder>> allocateOrderList = allocateOrderService.loadAllocateOrderList(allocateOrderLoadListRequest);
        if (Objects.isNull(allocateOrderList) || CollectionUtils.isEmpty(allocateOrderList.getResult())){
            return result;
        }
        AllocateOrder allocateOrder = allocateOrderList.getResult().get(0);
        AllocateOrderDetailLoadListRequest allocateOrderDetailLoadListRequest = new AllocateOrderDetailLoadListRequest();
        allocateOrderDetailLoadListRequest.setAllocateOrderId(allocateOrder.getId());
        allocateOrderDetailLoadListRequest.setLimit(MAX_PAGE_SIZE);
        Result<List<AllocateOrderDetail>> allocateOrderDetailList = allocateOrderDetailService.loadAllocateOrderDetailList(allocateOrderDetailLoadListRequest);
        TransferOrder transferOrder = transferOrder2AllocateOrderConvert.backward(allocateOrder, Optional.ofNullable(allocateOrderDetailList).map(Result::getResult).orElse(Lists.newArrayList()));
        return transferOrder == null ? result : Result.success(transferOrder);
    }
}
